package cn.iluwen.airline.Services;

import cn.iluwen.airline.Entities.Flight;
import cn.iluwen.airline.Entities.Seat;
import cn.iluwen.airline.Mapper.FlightMapper;
import cn.iluwen.airline.Mapper.SeatMapper;
import cn.iluwen.airline.utils.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 座位服务类
 * 处理座位相关的业务逻辑
 */
@Service
public class SeatService {
    
    @Autowired
    private SeatMapper seatMapper;
    
    @Autowired
    private FlightMapper flightMapper;
    
    /**
     * 获取所有座位
     * @return 座位列表
     */
    public List<Seat> getAllSeats() {
        return seatMapper.findAllSeats();
    }
    
    /**
     * 分页获取所有座位
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页后的座位列表
     */
    public PageInfo<Seat> getSeatsWithPaging(int pageNum, int pageSize) {
        PageUtils.startPage(pageNum, pageSize);
        List<Seat> seats = seatMapper.findSeatsWithPaging();
        return PageUtils.getPageInfo(seats);
    }
    
    /**
     * 根据ID获取座位
     * @param seatId 座位ID
     * @return 座位对象
     */
    public Seat getSeatById(String seatId) {
        return seatMapper.findById(seatId);
    }
    
    /**
     * 根据航班ID获取座位列表
     * @param flightId 航班ID
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页后的座位列表
     */
    public PageInfo<Seat> getSeatsByFlightId(String flightId, int pageNum, int pageSize) {
        PageUtils.startPage(pageNum, pageSize);
        List<Seat> seats = seatMapper.findByFlightId(flightId);
        return PageUtils.getPageInfo(seats);
    }
    
    /**
     * 批量获取航班座位和航班信息
     * @param flightId 航班ID
     * @return 包含座位列表和航班信息的Map
     */
    public Map<String, Object> getBatchSeatsByFlightId(String flightId) {
        Map<String, Object> result = new HashMap<>();
        
        // 获取航班信息
        Flight flight = flightMapper.findById(flightId);
        if (flight == null) {
            throw new RuntimeException("航班不存在: " + flightId);
        }
        
        // 获取座位列表
        List<Seat> seats = seatMapper.findByFlightId(flightId);
        
        result.put("flight", flight);
        result.put("seats", seats);
        
        return result;
    }
    
    /**
     * 根据航班ID和舱位等级获取座位列表
     * @param flightId 航班ID
     * @param seatClass 舱位等级
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页后的座位列表
     */
    public PageInfo<Seat> getSeatsByFlightIdAndClass(String flightId, String seatClass, int pageNum, int pageSize) {
        PageUtils.startPage(pageNum, pageSize);
        List<Seat> seats = seatMapper.findByFlightIdAndClass(flightId, seatClass);
        return PageUtils.getPageInfo(seats);
    }
    
    /**
     * 根据航班ID获取可用座位列表
     * @param flightId 航班ID
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页后的座位列表
     */
    public PageInfo<Seat> getAvailableSeatsByFlightId(String flightId, int pageNum, int pageSize) {
        PageUtils.startPage(pageNum, pageSize);
        List<Seat> seats = seatMapper.findAvailableSeatsByFlightId(flightId);
        return PageUtils.getPageInfo(seats);
    }
    
    /**
     * 添加座位
     * @param seat 座位对象
     * @return 添加后的座位对象
     */
    @Transactional
    public Seat addSeat(Seat seat) {
        // 生成UUID作为主键
        seat.setSeatId(UUID.randomUUID().toString());
        
        // 检查航班ID是否存在
        if (flightMapper.findById(seat.getFlightId()) == null) {
            throw new RuntimeException("航班不存在: " + seat.getFlightId());
        }
        
        // 检查座位号是否已存在
        Seat existingSeat = seatMapper.findByFlightIdAndSeatNumber(seat.getFlightId(), seat.getSeatNumber());
        if (existingSeat != null) {
            throw new RuntimeException("座位号已存在: " + seat.getSeatNumber());
        }
        
        seatMapper.insert(seat);
        return seat;
    }
    
    /**
     * 批量添加座位
     * @param seats 座位列表
     * @return 添加的座位数量
     */
    @Transactional
    public int batchAddSeats(List<Seat> seats) {
        for (Seat seat : seats) {
            // 生成UUID作为主键
            seat.setSeatId(UUID.randomUUID().toString());
            
            // 默认座位未被占用
            if (seat.getIsOccupied() == null) {
                seat.setIsOccupied(false);
            }
        }
        
        return seatMapper.batchInsert(seats);
    }
    
    /**
     * 更新座位信息
     * @param seat 座位对象
     * @return 更新后的座位对象
     */
    public Seat updateSeat(Seat seat) {
        // 检查是否修改了座位号，如果修改了，需要检查新座位号是否已存在
        Seat existingSeat = seatMapper.findById(seat.getSeatId());
        if (!existingSeat.getSeatNumber().equals(seat.getSeatNumber())) {
            Seat seatWithSameNumber = seatMapper.findByFlightIdAndSeatNumber(seat.getFlightId(), seat.getSeatNumber());
            if (seatWithSameNumber != null) {
                throw new RuntimeException("座位号已存在: " + seat.getSeatNumber());
            }
        }
        
        seatMapper.update(seat);
        return seat;
    }
    
    /**
     * 更新座位占用状态
     * @param seatId 座位ID
     * @param isOccupied 是否被占用
     * @return 是否更新成功
     */
    public boolean updateSeatOccupiedStatus(String seatId, boolean isOccupied) {
        int rows = seatMapper.updateOccupiedStatus(seatId, isOccupied);
        return rows > 0;
    }
    
    /**
     * 删除座位
     * @param seatId 座位ID
     * @return 是否删除成功
     */
    public boolean deleteSeat(String seatId) {
        int rows = seatMapper.delete(seatId);
        return rows > 0;
    }
    
    /**
     * 根据航班ID删除所有座位
     * @param flightId 航班ID
     * @return 删除的座位数量
     */
    public int deleteSeatsByFlightId(String flightId) {
        return seatMapper.deleteByFlightId(flightId);
    }
} 